#ifndef MESHMANAGER_CPP
#define MESHMANAGER_CPP

//TODO break all this managers in their own file in a new folder called manager

#include "meshmanager.h"

void CMeshManager::changeDrawingType(const ogl::CDrawingType::eBehaviour& eType)
{
    std::map<std::string,CMeshObj>::iterator it;
    for ( it=m_mesh_map.begin() ; it != m_mesh_map.end(); it++ ) (*it).second.changeDrawingType(eType);
}

GLuint CMeshManager::createBoxDisplayList(const Vector3d& center)
{
    GLuint box = glGenLists(1);
    glNewList(box,GL_COMPILE);
        glBegin( GL_QUADS);
            //première face ( Y +)
            glNormal3d(0,1,0);
            glTexCoord2f(1,1);  glVertex3d(center.X-0.5,center.Y+0.5,center.Z-0.5);
            glTexCoord2f(0,1);  glVertex3d(center.X+0.5,center.Y+0.5,center.Z-0.5);
            glTexCoord2f(0,0);  glVertex3d(center.X+0.5,center.Y+0.5,center.Z+0.5);
            glTexCoord2f(1,0);  glVertex3d(center.X-0.5,center.Y+0.5,center.Z+0.5);
            //troisième face ( X - )
            glNormal3d(-1,0,0);
            glTexCoord2d(0,0);  glVertex3d(center.X-0.5,center.Y+0.5,center.Z+0.5);
            glTexCoord2d(0,1);  glVertex3d(center.X-0.5,center.Y+0.5,center.Z-0.5);
            glTexCoord2d(1,1);  glVertex3d(center.X-0.5,center.Y-0.5,center.Z-0.5);
            glTexCoord2d(1,0);  glVertex3d(center.X-0.5,center.Y-0.5,center.Z+0.5);
            //quatrième face ( Z - )
            glNormal3d(0,0,-1);
            glTexCoord2d(1,1);  glVertex3d(center.X-0.5,center.Y+0.5,center.Z-0.5);
            glTexCoord2d(0,1);  glVertex3d(center.X+0.5,center.Y+0.5,center.Z-0.5);
            glTexCoord2d(0,0);  glVertex3d(center.X+0.5,center.Y-0.5,center.Z-0.5);
            glTexCoord2d(1,0);  glVertex3d(center.X-0.5,center.Y-0.5,center.Z-0.5);
            //cinquième face ( Y -)
            glNormal3d(0,-1,0);
            glTexCoord2d(0,1);  glVertex3d(center.X-0.5,center.Y-0.5,center.Z-0.5);
            glTexCoord2d(1,1);  glVertex3d(center.X+0.5,center.Y-0.5,center.Z-0.5);
            glTexCoord2d(1,0);  glVertex3d(center.X+0.5,center.Y-0.5,center.Z+0.5);
            glTexCoord2d(0,0);  glVertex3d(center.X-0.5,center.Y-0.5,center.Z+0.5);
            //sixième face ( X + )
            glNormal3d(1,0,0);
            glTexCoord2d(0,1);  glVertex3d(center.X+0.5,center.Y-0.5,center.Z-0.5);
            glTexCoord2d(1,1);  glVertex3d(center.X+0.5,center.Y+0.5,center.Z-0.5);
            glTexCoord2d(1,0);  glVertex3d(center.X+0.5,center.Y+0.5,center.Z+0.5);
            glTexCoord2d(0,0);  glVertex3d(center.X+0.5,center.Y-0.5,center.Z+0.5);
        glEnd();
    glEndList();
    return box;
}

CMeshObj* CMeshManager::getMeshPtr(const std::string& mesh_name)
{
    return &m_mesh_map.find(mesh_name)->second;
}

void CMeshManager::loadAllMesh()
{
    m_mesh_map["CPlayer"]=CMeshObj();
    m_mesh_map.find("CPlayer")->second.loadObj("blender/seamed_suzanne.obj");
    m_mesh_map["snake"]=CMeshObj();
    m_mesh_map.find("snake")->second.loadObj("blender/pruzina.obj");
}

#endif //MESHMANAGER_CPP
